<template>
  <f7-app :params="f7params">
    <!-- Current View/Router, initial page will be loaded from home.vue component -->
    <f7-view main url="/"></f7-view>


  </f7-app>
</template>

<script>
import routes from './router/index.js'


// import Framework7 from 'framework7/framework7.esm.bundle.js'
// var app = new Framework7();
var accessToken = '';

export default {
  data () {
    return {
      // Framework7 parameters here
      f7params: {
        id: 'io.framework7.testapp', // App bundle ID
        name: 'Framework7', // App name
        theme: 'auto', // Automatic theme detection
         // App routes
        routes: routes
       }
    }
  },
  created () {
    this.$f7ready((f7) => {
      let that = this;
      this.conveyCookie(f7);
      // f7.request.get(this.GLOBAL.RequestAddress + '/logout', null, null);

      f7.request.post(this.GLOBAL.RequestAddress + '/login.jsp', { username: '赵明明', password: '123' }, function (data) {
        let json = JSON.parse(data);
        if (json.success) {
          sessionStorage.setItem('accessToken', json.access_token);
          sessionStorage.setItem('userToken', json.user.token);
          sessionStorage.setItem('userId', json.user.id)
          that.getOrgNames(f7);
        }
      });
    });
  },
  methods : {
    conveyCookie : function (app) {
      let that = this;
      let accessToke = that.getCookie("accessToken");
      let userToke   = that.getCookie("userToken");
      let userId   = that.getCookie("userId");
      let userName   = that.getCookie("userName");
      sessionStorage.setItem("accessToken", accessToke);
      sessionStorage.setItem("userToken", userToke);
      sessionStorage.setItem('userId', userId)
      sessionStorage.setItem('userName', userName)
      that.getOrgNames(app);
      console.log("atken:" + accessToke + "  uken:" + userToke)
    },
    getCookie:  function (c_name){
      let doc = document;
      if (doc.cookie.length>0){
        let c_start=doc.cookie.indexOf(c_name + "=")
        if (c_start!=-1){
          c_start=c_start + c_name.length+1
          let c_end=doc.cookie.indexOf(";",c_start)
          if (c_end==-1) c_end=doc.cookie.length
          return unescape(doc.cookie.substring(c_start,c_end))
        }
      }
      return ""
    },
    getOrgNames: function (f7) {
      let that = this;
      f7.request.setup({
        headers: {
          'AccessToken': sessionStorage.getItem('accessToken'),
          'JSESSIONID': sessionStorage.getItem('userToken'),
        }
      });
      f7.request.json(that.GLOBAL.RequestAddress + '/camel/rest/users/' + sessionStorage.getItem('userId') + "/orgs",{
          'page':1,
          'start':0,
          'limit':20
        }, function(res){
          if(res.totalCount>0){
            that.GLOBAL.orgNames = res.data;
          }
        }
      );
      f7.request.json(that.GLOBAL.RequestAddress + '/camel/rest/meetingrooms',{
          'page':1,
          'start':0,
          'limit':20
        }, function(res){
          if(res.totalCount>0){
            that.GLOBAL.meetingRooms = res.data;
          }
        }
      );
    }
  }
}

</script>
